package com.manager.container.service.impl;

import com.manager.common.customer.utils.RemoteExecute;
import com.manager.common.dao.SuperDao;
import com.manager.common.utils.ObjectUtils;
import com.manager.common.vo.PageVO;
import com.manager.common.vo.ResultVO;
import com.manager.container.service.DockerImagesService;
import com.manager.model.DockerImages;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DockerImagesServiceImpl extends SuperDao<DockerImages> implements DockerImagesService {

    @Override
    public PageVO getImagePage(DockerImages model) {
        List<Object> params = new ArrayList<>();
        String sql = " from DockerImages t where 1 = 1 ";
        if(StringUtils.isNotEmpty(model.getName())){
            sql += " and t.name like # ";
            params.add("%"+model.getName()+"%");
        }
        if(StringUtils.isNotEmpty(model.getTag())){
            sql += " and t.tag like # ";
            params.add("%"+model.getTag()+"%");
        }
        sql = ObjectUtils.replaceIndexByList(sql,params);

        List<DockerImages> list = this.getList(sql,params.toArray(),model.getStart(),model.getLimit(),"");
        int sum = this.getListCount("select count(*) " + sql,params.toArray());
        int count = (int)Math.ceil(Double.valueOf(sum)/Double.valueOf(model.getLimit()));
        return PageVO.create(model.getStart(),count,ObjectUtils.getJSONArray(DockerImages.config,list));
    }

    @Override
    public void addImages(DockerImages model) {
        this.addT(model);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateImages(DockerImages model) throws Exception {
        DockerImages dockerImages = this.getT(DockerImages.class,model.getId());
        boolean hasUpdate = (!model.getName().equals(dockerImages.getName()) || !model.getTag().equals(dockerImages.getTag()) && dockerImages.getPushStatus() == 1) ;
        if(hasUpdate){
            String command = " docker tag  " + dockerImages.getImageId() +" " + model.getName() + ":" + model.getTag() + " " ;
            String cmd = " docker rmi " + dockerImages.getName() + ":" + dockerImages.getTag() + " " ;
            ResultVO resultVO = RemoteExecute.executeSsh(command,cmd);
            if(resultVO.getCode() != 1){
                throw new Exception("调用执行ssh接口失败!");
            }
        }
        dockerImages.setName(model.getName());
        dockerImages.setTag(model.getTag());
        dockerImages.setFilePath(model.getFilePath());
        dockerImages.setFileSize(model.getFileSize());
        this.updateT(dockerImages);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteImages(DockerImages model) throws Exception{
        DockerImages dockerImages = this.getT(DockerImages.class,model.getId());
        String cmd = "docker rmi " + dockerImages.getImageId();
        ResultVO resultVO = RemoteExecute.executeSsh(cmd);
        if(resultVO.getCode() != 1){
            throw new Exception("调用执行ssh接口失败!");
        }
        this.delete(dockerImages);
    }

    @Override
    public List<DockerImages> getImageList(DockerImages model) {
        String sql = " from DockerImages t  ";
        List<DockerImages> list = this.getList(sql,0,0,"");
        return list;
    }

    @Override
    public DockerImages getImage(DockerImages model) {
        return this.getT(DockerImages.class,model.getId());
    }

    @Override
    public void updateImageId(DockerImages model) {
        DockerImages dockerImages = this.getT(DockerImages.class,model.getId());
        dockerImages.setImageId(model.getImageId());
        dockerImages.setPushStatus(1);
        this.updateT(dockerImages);
    }
}
